<template>
	<el-dialog title="物流发货" width="25%" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit('dataForm')" label-width="80px">
			<el-form-item label="快递公司" prop="express_code">
				<el-select class="w-100" v-model="dataForm.express_code" @change="selectChange" placeholder="请选择快递公司" clearable filterable>
					<el-option v-for="item in options" :key="item.logistics_code" :label="item.company" :value="item.logistics_code">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="物流单号" prop="logistic_code">
				<el-input maxlength="50" v-model="dataForm.logistic_code" placeholder="请输入快递单号"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit('dataForm')">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
import {
	treeDataTranslate
} from '@/utils'
import orderService from '@/api/orderService.js'
export default {
	data () {
		return {
			visible: false,
			type: '',
			dataForm: {
				express_code: '',
				logistic_code: ''
			},
			options: [],
			dataRule: {
				express_code: [{
					required: true,
					message: '请先选择快递公司',
					trigger: 'change'
				}],
				logistic_code: [{
					required: true,
					message: '快递单号不能为空',
					trigger: 'blur'
				}]
			}
		}
	},
	created () {
		this.goodStreamList()
	},

	methods: {
		init (item, type) {
			this.type = type || ''
			if (type == 'update') {
				this.deliveryDetail(item.order_id, item.id)
				console.log(item)
			} else {
				this.dataForm = {
					express_code: '',
					logistic_code: ''
				}
				this.dataForm.order_id = item.id || null
			}
			this.visible = true
		},

		goodStreamList () {
			orderService.goodStreamList().then(res => {
				if (res.data && res.data.code == 200) {
					this.options = res.data.list
				}
			})
		},
		/* 发货详情 */
		deliveryDetail (order_id, id) {
			orderService.deliveryDetail(order_id, id).then(res => {
				if (res.data && res.data.code == 200) {
					if (res.data.data.express) {
						this.dataForm = res.data.data.express
					}
					this.dataForm.order_id = res.data.data.order_id
					this.dataForm.package_id = res.data.data.id
				}
			})
		},

		dataFormSubmit (e) {
			this.$refs[e].validate((valid) => {
				if (valid) {
					if (this.type) {
						// 修改
						orderService.updateDelivery(this.dataForm).then(res => {
							if (res.data && res.data.code == 200) {
								this.$message.success('修改信息成功！')
								this.$emit('result', res.data.data)
							} else {
								this.$message.error(res.data.msg)
							}
						})
					} else {
						orderService.delivery(this.dataForm).then(res => {
							if (res.data && res.data.code == 200) {
								this.$message.success('发货成功！')
								this.$emit('result', res.data.data)
							} else {
								this.$message.error(res.data.msg)
							}
						})
					}
				} else {
					console.log('error submit!!')
					return false
				}
			})
		},

		selectChange (e) {
			for (let item of this.options) {
				if (e == item.logistics_code) {
					this.dataForm.express_name = item.company
					break
				}
			}
		}
	}
}
</script>

<style lang="scss"></style>
